package com.net.noah.core.service.impl;

import com.net.noah.core.domain.SerialNumber;
import com.net.noah.core.manager.SerialNumberManager;
import com.net.noah.core.service.SerialNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 2020/3/12 09:35

 */
@Service
public class SerialNumberGeneratorImpl implements SerialNumberGenerator {

    @Autowired
    private SerialNumberManager serialNumberManager;

    @Override
    @Transactional(transactionManager = "noahCommonTransactionManager", propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public SerialNumber getSerialNumber(String name) {
        SerialNumber serialNumber = serialNumberManager.getByNameWithLock(name);
        serialNumber.setNextVal(serialNumber.getNextVal() + serialNumber.getStep());
        serialNumberManager.update(serialNumber);
        //跟新完减回来
        serialNumber.setNextVal(serialNumber.getNextVal() - serialNumber.getStep());
        return serialNumber;
    }
}
